package com.huyaanalysisplatform.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huyaanalysisplatform.common.Result;
import com.huyaanalysisplatform.entity.TUser;
import com.huyaanalysisplatform.mapper.TUserMapper;
import com.huyaanalysisplatform.service.TUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Huadi Group 12
 * @since 2021-08-01
 */
@Service
public class TUserServiceImpl extends ServiceImpl<TUserMapper, TUser> implements TUserService {
    @Autowired
    TUserMapper tUserMapper;

    @Override
    public Result register(TUser tUser) {

        TUser sameName = tUserMapper.selectByName(tUser.getName());
        TUser samePhone = tUserMapper.selectByPhone(tUser.getPhone());
        TUser sameEmail = tUserMapper.selectByEmail(tUser.getEmail());
        if(sameName != null) return Result.error().code(3001);  //用户名被占用
        if (samePhone != null) return Result.error().code(4001); //手机号被占用
        if(sameEmail != null) return Result.error().code(5001); //邮箱被占用
        tUser.setId(UUID.randomUUID().toString().replace("-",""));
        this.save(tUser);
        return Result.success().code(2001);
    }

    @Override
    public Result loginByEmail(String email, String password) {
        TUser user = tUserMapper.selectByEmail(email);
        if(user == null) {
            return Result.error().code(3001);
        }
        if(!user.getPassword().equals(password)){
            return Result.error().code(4001);
        }
        return Result.success().code(2001).data("user",user);
    }

    @Override
    public Result loginByPhone(String phone, String password) {
        TUser user = tUserMapper.selectByPhone(phone);
        if(user == null) {
            return Result.error().code(3001);
        }
        if(!user.getPassword().equals(password)){
            return Result.error().code(4001);
        }
        return Result.success().code(2001).data("user",user);
    }

    @Override
    public Result edit(TUser tUser) {
        TUser tUserSelectById = tUserMapper.selectById(tUser.getId());
        if (!tUserSelectById.getName().equals(tUser.getName())){
            TUser sameName = tUserMapper.selectByName(tUser.getName());
            if(sameName != null) return Result.error().code(3001);  //用户名被占用
        }
        if(!tUserSelectById.getPhone().equals(tUser.getPhone())){
            TUser samePhone = tUserMapper.selectByPhone(tUser.getPhone());
            if (samePhone != null) return Result.error().code(4001); //手机号被占用
        }
        if(!tUserSelectById.getEmail().equals(tUser.getEmail())){
            TUser sameEmail = tUserMapper.selectByEmail(tUser.getEmail());
            if(sameEmail != null) return Result.error().code(5001); //邮箱被占用
        }
        this.updateById(tUser);
        return Result.success().code(2001);
    }


}
